
var gplus_rs = [];
var DbRs = function(fsuc,ferr){
    this.funcsuc = fsuc;
    this.funcerr = ferr;
};
function Plus_db_suc(id, data){
    gplus_rs[ id ].funcsuc(data);
    delete  gplus_rs[ id ];
};
function Plus_db_err(id, data){
    gplus_rs[ id ].funcerr(data);
    delete  gplus_rs[ id ];
};

var GapPlus = function() {
};

//初始化
//返回：各种参数
GapPlus.prototype.init = function(win, fail) {
  PhoneGap.exec(win, fail, "Plus", "init", ['NONE']);
};

//Toast
GapPlus.prototype.msg = function(txt, win, fail) {
  PhoneGap.exec(win, fail, "Plus", "toast", [txt]);
};

//inputText
//一个有EditText的对话框
//输入：初始的文本
//返回：输入的文本
GapPlus.prototype.inputText = function(txt, win, fail) {
  PhoneGap.exec(win, fail, "Plus", "inputText", [txt]);
};
//datepicker
//日期选择对话框
//输入: 如果year=month=day= <=0使用默认当前日期
//返回: 对象{year,month,day,text}, text类似2011-01-23
GapPlus.prototype.datePicker = function(year,month,day, win, fail) {
  var tmpObj = {"year":year, "month":month,"day":day};
  PhoneGap.exec(win, fail, "Plus", "datePicker", [tmpObj]);
};
//timepicker
//时间选择对话框
//输入: 如果hour=minute=-1使用默认当前时间
//返回: 对象{hour,minute,text}, text类似12:09
GapPlus.prototype.timePicker = function(hour,minute, win, fail) {
  var tmpObj = {"hour":hour, "minute":minute};
  PhoneGap.exec(win, fail, "Plus", "timePicker", [tmpObj]);
};

//文件或者文件夹是否存在
GapPlus.prototype.exists = function(url, win, fail) {
  PhoneGap.exec(win, fail, "Plus", "exists", [url]);
};

//创建文件夹
GapPlus.prototype.mkdir = function(dir, win, fail) {
  PhoneGap.exec(win, fail, "Plus", "mkdir", [dir]);
};

//移动文件, From: URL or URI
GapPlus.prototype.move = function(From, To, win, fail) {
  PhoneGap.exec(win, fail, "Plus", "move", [From, To]);
};

//拷贝文件 From: URL or URI
GapPlus.prototype.copy = function(From, To, win, fail) {
  PhoneGap.exec(win, fail, "Plus", "copy", [From, To]);
};

//选择图像
//type = 0 图库
//type = 1 相机
GapPlus.prototype.getPic = function(type, win, fail) {
  PhoneGap.exec(win, fail, "Plus", "getPic", [type]);
};


//图像剪裁
//From: 来源图片的路径或者URI
//To :  保存图片的路径或者URI
//Opt: {} 参数，对象
//     outputX, 横轴默认宽度, 96
//     outputY, 竖轴默认宽度, 96
//     aspectX, 横轴默认比例, 1
//     aspectY, 竖轴默认比例, 1

GapPlus.prototype.cropPic = function(From, To, win, fail) {
  PhoneGap.exec(win, fail, "Plus", "cropPic", [From, To, {}]);
};
GapPlus.prototype.cropPicOpt = function(From, To, Opt, win, fail) {
  PhoneGap.exec(win, fail, "Plus", "cropPic", [From, To, Opt]);
};

//图像等比例压缩 From: URL or URI
//inSampleSize，缩小到原来的? >=1
GapPlus.prototype.inSampleSizePic = function(From, To, inSampleSize, win, fail) {
  PhoneGap.exec(win, fail, "Plus", "inSampleSizePic", [From, To, inSampleSize]);
};

//图像等比例压缩，指定一个边长
GapPlus.prototype.resizePic = function(From, To, len, win, fail) {
  PhoneGap.exec(win, fail, "Plus", "resizePic", [From, To, len]);
};

//分享文本
GapPlus.prototype.shareText = function(subject, contents, win, fail) {
  PhoneGap.exec(win, fail, "Plus", "sendText", [subject, contents]);
};

//分享图片
GapPlus.prototype.sharePic = function(url, win, fail) {
  PhoneGap.exec(win, fail, "Plus", "sendImage", [url]);
};

//初始化数据库
GapPlus.prototype.initDB = function(win, fail) {
  PhoneGap.exec(win, fail, "Plus", "initDB", ["NONE"]);
};

//查询数据库
GapPlus.prototype.queryDB = function(sql, win, fail) {
  var uuid = PhoneGap.createUUID();
  gplus_rs[ uuid ] = new DbRs(win,fail);
  
  PhoneGap.exec(null, null, "Plus", "queryDB", [sql,uuid]);
  //PhoneGap.exec(win, fail, "Plus", "queryDB", [sql]);
};

GapPlus.prototype.selDB = function(dbname, where, win, fail) {
  var uuid = PhoneGap.createUUID();
  gplus_rs[ uuid ] = new DbRs(win,fail); 
  PhoneGap.exec(null, null, "Plus", "selectDB", [dbname, where, uuid]);   
    
  //PhoneGap.exec(win, fail, "Plus", "selectDB", [dbname, where]);
};
GapPlus.prototype.newDB = function(dbname, vals, win, fail) {
  PhoneGap.exec(win, fail, "Plus", "insertDB", [dbname, vals]);
};
GapPlus.prototype.delDB = function(dbname, where, win, fail) {
  PhoneGap.exec(win, fail, "Plus", "deleteDB", [dbname, where]);
};

//备份数据库
GapPlus.prototype.backupDB = function(win, fail) {
  PhoneGap.exec(win, fail, "Plus", "backupDB", ["NONE"]);
};

//恢复数据库
GapPlus.prototype.restoreDB = function(win, fail) {
  PhoneGap.exec(win, fail, "Plus", "restoreDB", ["NONE"]);
};


PhoneGap.addConstructor(function() { 
  // Register the javascript plugin with PhoneGap
  PhoneGap.addPlugin('Plus', new GapPlus());
  // Register the native class of plugin with PhoneGap
  navigator.app.addService("Plus", "com.xiaoguo.plugin.Plus");
});

